import requests
import cv2
import numpy as np
class VideoStreamClient:
    def __init__(self, server_url="http://localhost:5000"):
        self.server_url = server_url

    def get_frame(self, stream_id):
        url = f"{self.server_url}/frame/{stream_id}"
        try:
            r = requests.get(url, timeout=5)
            if r.status_code == 200:
                arr = np.frombuffer(r.content, np.uint8)
                return cv2.imdecode(arr, cv2.IMREAD_COLOR)
        except:
            pass
        return None

# 使用
client = VideoStreamClient()
while True:
    frame = client.get_frame("camera_1")
    if frame is not None:
        cv2.imshow("Camera 1", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
